<?php
	include_once 'metier/eleve.dao.mysql.php';
	include_once 'metier/eleve.class.php';
	        

$db = new myPDO();

         $tmpid = $_POST['id_eleve'];
	$tmpNom = $_POST['nom_eleve'];
	$tmpPrenom = $_POST['prenom_eleve'];
        if ($_FILES["url_photo_eleve"]["name"] == "") {
            // si l'élève n'a pas de photo
            $tmpimage="";
        }
        else {
            $extension = substr($_FILES["url_photo_eleve"]["name"], strrpos($_FILES["url_photo_eleve"]["name"], '.') + 1);
            $tmpimage = $_POST['nom_eleve'] . '.' . $_POST['prenom_eleve'] . '.' . $extension;
        }


	$eleve = new Eleve($tmpid, $tmpNom, $tmpPrenom, "", $tmpimage);
        $eleveDAO = new EleveDAO();
 
        if($eleveDAO->findOtherEleve($eleve, $db) == 0) {
            
            // si l'élève n'existe pas déjà
            $el = new Eleve ($tmpid);
            $elDAO = new EleveDAO ();
            $eleve_img = $elDAO->findEleve ($el,$db);

            $img = $eleve_img->getImage();

            $file = "../imagesProfils" ;
            if($tmpimage==""){
                $extension2 = substr($img, strrpos($img, '.') + 1);
                $tmpimage=$tmpNom.'.'.$tmpPrenom.'.'.$extension2;
                rename(utf8_decode($file."/".$img), utf8_decode($file."/".$tmpimage));
            }
            else {
            unlink (utf8_decode($file."/".$img));

            $uploads_dir = "../imagesProfils/" . $tmpimage;
            move_uploaded_file($_FILES['url_photo_eleve']['tmp_name'], utf8_decode($uploads_dir));
            }
            $eleve = new Eleve($tmpid, $tmpNom, $tmpPrenom, "", $tmpimage);
            $eleveDAO->updateEleve($eleve, $db);
            
            header('Location: ../partie_generateur/ens_gestion_eleves.html');
        }
        else {
            echo utf8_decode('<script language="Javascript">
            alert ("Attention; l\'utilisateur existe déjà !" )
            window.location = "../partie_generateur/ens_gestion_eleves.html";
          </script>');
        }
?>